package cn.cttic.base.service.impl;

import cn.cttic.base.api.domain.BaseDeviceInfo;
import cn.cttic.base.api.domain.BaseFieldWorkProcessGroup;
import cn.cttic.base.api.domain.BaseProduct;
import cn.cttic.base.api.domain.vo.BaseDeviceVo;
import cn.cttic.base.api.domain.vo.BaseFieldWorkProcessGroupVo;
import cn.cttic.base.api.domain.vo.BaseProductVo;
import cn.cttic.base.mapper.BaseProductMapper;
import cn.cttic.base.service.IBaseFieldWorkProcessGroupService;
import cn.cttic.common.core.exception.base.BaseException;
import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.stereotype.Service;
import cn.cttic.base.api.domain.bo.BaseDeviceBo;
import cn.cttic.base.api.domain.BaseDevice;
import cn.cttic.base.mapper.BaseDeviceMapper;
import cn.cttic.base.service.IBaseDeviceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备型号Service业务层处理
 *
 * @author TImes.Studio
 * @date 2024-09-17
 */
@RequiredArgsConstructor
@Service
public class BaseDeviceServiceImpl implements IBaseDeviceService {

    private final BaseDeviceMapper baseMapper;

    private final BaseProductMapper baseProductMapper;

    /**
     * 查询设备型号
     *
     * @param id 主键
     * @return 设备型号
     */
    @Override
    public BaseDeviceVo queryById(String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询设备型号列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 设备型号分页列表
     */
    @Override
    public TableDataInfo<BaseDeviceVo> queryPageList(BaseDeviceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseDevice> lqw = buildQueryWrapper(bo);
        Page<BaseDeviceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的设备型号列表
     *
     * @param bo 查询条件
     * @return 设备型号列表
     */
    @Override
    public List<BaseDeviceVo> queryList(BaseDeviceBo bo) {
        LambdaQueryWrapper<BaseDevice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseDevice> buildQueryWrapper(BaseDeviceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseDevice> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseDevice::getCreateTime);
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), BaseDevice::getDeviceName, bo.getDeviceName());
        lqw.like(StringUtils.isNotBlank(bo.getDeviceModel()), BaseDevice::getDeviceModel, bo.getDeviceModel());
        lqw.like(bo.getDevicePrice() != null, BaseDevice::getDevicePrice, bo.getDevicePrice());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceSize()), BaseDevice::getDeviceSize, bo.getDeviceSize());
        lqw.eq(StringUtils.isNotBlank(bo.getDevicePoster()), BaseDevice::getDevicePoster, bo.getDevicePoster());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceThumbnail()), BaseDevice::getDeviceThumbnail, bo.getDeviceThumbnail());
        lqw.eq(bo.getDeviceStockCount() != null, BaseDevice::getDeviceStockCount, bo.getDeviceStockCount());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceDescription()), BaseDevice::getDeviceDescription, bo.getDeviceDescription());
        return lqw;
    }

    /**
     * 新增设备型号
     *
     * @param bo 设备型号
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseDeviceBo bo) {
        BaseDevice add = MapstructUtils.convert(bo, BaseDevice.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改设备型号
     *
     * @param bo 设备型号
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseDeviceBo bo) {
        BaseDevice update = MapstructUtils.convert(bo, BaseDevice.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseDevice entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除设备型号信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            for (String id : ids) {
                List<BaseProductVo> productVoList = baseProductMapper.selectVoList(
                    new LambdaQueryWrapper<BaseProduct>()
                        .eq(BaseProduct::getDeviceId, id)
                );
                if (productVoList.size() > 0) {
                    throw new BaseException("该设备存在关联的产品套餐，无法删除");
                }
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
